package com.yf1706.tools;

import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;

import com.yf1706.bean.Dep;
import com.yf1706.bean.Emp;
import com.yf1706.bean.User;
import com.yf1706.service.UserService;

@Component
public class UserTools {
	private final static String USER_FORMAT="00000";
	private final static String DEP_FORMAT="000";
	private final static String PREFIX="s";
	public static void main(String[] args) {
		System.out.println("userIdFormat:"+userIdFormat(2));
		System.out.println("depIdFormat:"+depIdFormat(4));
		System.out.println("depIdUnformat:"+depIdUnformat(depIdFormat(4)));
		System.out.println("userIdUnformat:"+userIdUnformat(userIdFormat(2)));
		String fileName = "12";
        System.out.println("================  前补零方法一   =================");
        DecimalFormat g1=new DecimalFormat("0000000");
        String startZeroStr = g1.format(Integer.valueOf(fileName));
        System.out.println("前补零方法一："+startZeroStr);

        System.out.println("================  前补零方法二   =================");
        startZeroStr = String.format("%07d",Integer.valueOf(fileName));
        System.out.println("前补零方法二："+startZeroStr);

        System.out.println("================  后补零方法一   =================");
        DecimalFormat g2=new DecimalFormat("0.000000");
        String endZeroStr = g2.format(Integer.valueOf(fileName));
        System.out.println("后补零："+endZeroStr);
        System.out.println("虽然后补零出现这种情况,带有小数点");
        System.out.println("比如你要长度要在7位以内，可以这么做");
        System.out.println("后补零转变后："+endZeroStr.replace(".","").substring(0,7));
        
        
        UserService us=new UserService();
        if(us.login(1, "123")==null) {
        	System.out.println(us.login(1, "123"));
        	
        }else {
        	System.out.println("no");
        }
	}
	
	//将原始用户编号自动补0并加前缀
	public static String userIdFormat(int id) {
		DecimalFormat g1=new DecimalFormat(USER_FORMAT);
        String startZeroStr = PREFIX+g1.format(Integer.valueOf(String.valueOf(id)));
        return startZeroStr;
	}
	public static List<Emp> listEmpIdFormat(List<Emp> list) {
		for(Iterator<Emp> it=list.iterator();it.hasNext();) {
			Emp emp=it.next();
			emp.setEmpNum(userIdFormat(emp.getEmpId()));
		}
        return list;
	}
	
	//将原始部门编号自动补0
	public static String depIdFormat(int id) {
		DecimalFormat g1=new DecimalFormat(DEP_FORMAT);
        String startZeroStr = g1.format(Integer.valueOf(String.valueOf(id)));
        return startZeroStr;
	}
	public static List<Dep> listDepIdFormat(List<Dep> list) {
		for(Iterator<Dep> it=list.iterator();it.hasNext();) {
			Dep dep=it.next();
			dep.setDepNum(depIdFormat(dep.getDepId()));
		}
        return list;
	}
	
	//将用户编号去0
	public static int userIdUnformat(String id) {
		if(id.length()!=USER_FORMAT.length()+1||!id.startsWith("s")) {
			return -1;
		}
		String postfix=id.substring(1);
		return Integer.parseInt(postfix);
	}
	
	//将部门编号去0
	public static int depIdUnformat(String id) {
		if(id.length()!=DEP_FORMAT.length()+1) {
			return -1;
		}
		return Integer.parseInt(id);
	}
	
	/**
	 * 返回当前登录的用户对象
	 * @param response
	 */
	public static User getUser(HttpServletRequest request) {
		return (User) request.getSession().getAttribute("user");
	}
	
}
